/// @FUNCTION
/// @NAME XfMissionCreateMissionUnit
/// @DESC 
/// @ACCESS Server
/// @MP False
/// @PARAM String Name of the unit type
/// @PARAM Variant A side string or a group object
/// @PARAM Position Either a position or a combined position and direction [[pos],dir]
/// @PARAM String The action type of the object: REGISTER, ASSASSINATE, DESTROY, etc. @DEFAULT "REGISTER"
/// @PARAM Array The actions parameters for the given action. @DEFAULT []
/// @PARAM String The unit init line @DEFAULT ""
/// @PARAM String The unit special creation parameter (NONE, FORM, FLY, etc.) @DEFAULT "NONE"
/// @RETURN Object The created object
/// @REQUIRE Domination Template
/// @REMARK 
/// @EXAMPLE [["RU_Profiteer2",_grp,[5892.23,10198.2,0],"doStop this;"] ,"ASSASSINATE" ,["You killed the traitor","jip_traitor_killed"]] call XfMissionCreateMissionUnit;

//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "framework\fn_createMissionUnit.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>
#include <ACV_constantes.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_obj_params","_obj_type","_obj_group","_obj_pos","_obj_dir","_framework_action","_obj_initline","_obj_special","_framework_params","_obj","_crew","_acv_params","_grp","_instance_id"
		,"_obj_identity"];
DEFAULT_PARAM(_obj_params,TMF_CREATION_OBJPARAMS,"");
DEFAULT_PARAM(_framework_action,TMF_CREATION_ACTION,"REGISTER");
DEFAULT_PARAM(_framework_params,TMF_CREATION_ACTPARAMS,[]);
DEFAULT_PARAM(_instance_id,TMF_CREATION_INSTANCE,TMF_SERVER_INSTANCE_ID);

if (_instance_id == TMF_SERVER_INSTANCE_ID && isMultiplayer && !isDedicated && !isnull player) then { _instance_id = getplayerUID player;};

_obj_type = 	GETVAL(_obj_params,TMF_VEH_PARAMS_TYPE,"");
_obj_pos = 		GETVAL(_obj_params,TMF_VEH_PARAMS_POS,ARR_3(0,0,0));
_obj_group =	GETVAL(_obj_params,TMF_VEH_PARAMS_GRP,grpNull);
_obj_initline =	GETVAL(_obj_params,TMF_VEH_PARAMS_INIT,[]);
_obj_special = 	GETVAL(_obj_params,TMF_VEH_PARAMS_SPEC,"NONE");
_obj_identity =	GETVAL(_obj_params,TMF_VEH_PARAMS_IDENTITY,"");

// Case where we receive a marker as the position
if (typename _obj_pos == typename "") then {
	_obj_dir = markerDir _obj_pos;
	_obj_pos = markerPos _obj_pos;
};
// Case where we receive the position plus the direction.
if (typename (_obj_pos select 0) == "ARRAY") then {
	_obj_dir = _obj_pos select 1;
	_obj_pos = _obj_pos select 0;
};	
_framework_action = ToUpper(_framework_action);

_obj = objNull;
DEBUG_1("Creation Params",ARR_8(_obj_type,_obj_group,_obj_pos,_framework_action,_framework_params,_obj_initline,_obj_special,(_obj_type isKindOf "Man")));

// Object position correction
if ((_obj_pos select 2) < 0) then {_obj_pos set [2,0]};

// Check if the object already exists
If (typeName _obj_type == typename objNull) then {
	_obj = _obj_type;
	_obj_type = typeOf _obj;
	_obj_group = group _obj;
} else {
	// Creating using the mission framework
	// Convert from Side string to a new group
	If (typeName _obj_group == typename "") then {
		_obj_group = switch (_obj_group) do {
			case "EAST": {_obj_group};
			case "WEST": {_obj_group};
			case "GUER": {_obj_group};
			case "CIV": {_obj_group};
			default {GVAR(enemy_side)};
		};
		_obj_group = [_obj_group] call CFUNC(createGroup);
	};
	if (_obj_type isKindOf "Man") then {
		_obj = [_obj_type, _obj_group, _obj_pos, 0] call CFUNC(createUnit);
		DEBUG_1("Creating",ARR_3(group _obj, typeof _obj, _obj));
		_obj setVariable ["BIS_noCoreConversations", true];
	} else {
		_obj = createVehicle [_obj_type, _obj_pos, [], 0, _obj_special];
		//TODO: Create Crew
		//_crew = [_obj, _obj_group] call XfSpawnCrew;
	};
	_obj setPos _obj_pos;
	if (!isNil "_obj_dir") then {_obj setDir _obj_dir;};
	// Registering in the ACV if necessary and if possible
	if (!isnil "ACV_FNCT_PutInStock") then {
		_obj_name = _obj_type;
		if (typename (_framework_params) == typename []) then {
			if (count _framework_params > 0) then {
				if ((typename (_framework_params select 0) == typename "") AND (_framework_action == "PICKUP")) then {_obj_name = _framework_params select 0;};
			};
		};
		_type = 0;
		if (_obj_type isKindOf "Man") then {
			_type = ACV_NET_MAKEUNITS;
		} else {
			_type = ACV_NET_MAKEVEHICLES;
		};
		[_type, _obj_group, _obj_name, _instance_id] call ACV_FNCT_PutInStock;
		[ format[localize "STR_MSG_CREATEDBY",_obj_name, _instance_id]] call ACV_FNCT_BroadcastMessage;
	};
};
[_obj_group,_instance_id] call FFUNC(setInstanceID);

if (_obj_identity != "" && _obj isKindOf "Man") then {
	_obj setIdentity _obj_identity;
};

// We set the action and parameters for the registration
[_obj,_instance_id] call FFUNC(setInstanceID);
_action = call CFUNC(hashEmpty);
_action = [_action, (call CFUNC(nextJipID))] call FFUNC(actionSetID);
_action = [_action, _framework_action] call FFUNC(actionSetType);
_action = [_action, _framework_params] call FFUNC(actionSetParams);
_action = [_action, _obj_params] call FFUNC(actionSetObjParams);
_action = [_action, _instance_id] call FFUNC(actionSetInstance);
[_obj,_action] call FFUNC(actionSet);

// We register the object on all systems
[QGVAR(all_object),[_obj,_action],THIS_FILE_] call NFUNC(callEvent);

TRACE_1("End",ARR_2(_obj, typeof _obj));
TMFPROFILERSTOP;
// We return the object.
_obj;
